//
//  HMNetworkTools.swift
//  01-Swift网络框架封装
//
//  Created by HM on 16/5/22.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
//导入命名空间
import AFNetworking


//声明枚举
//枚举任意类型
enum HTTPMethods: Int {
    case POST
    case GET
}


struct API {
    
    static let hostName = "https://api.weibo.com/"
    //    static let hostName = "https://dev.api.weibo.com/"
    //加载用户token
    static let loadAccessToken = "oauth2/access_token"
    //加载用户信息
    static let loadUserInfo = "2/users/show.json"
    
    //加载首页数据
    static let loadHomedata = "2/statuses/home_timeline.json"
    
}

class HMNetworkTools: AFHTTPSessionManager {
    //声明单例对象
    static let sharedTools: HMNetworkTools = {
        //把url前面一样的部分 声明为基础的url地址
        
        let url = NSURL(string: API.hostName)
        let tools = HMNetworkTools(baseURL: url)
           
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    
    
    //封装 get & post 请求方法 
    
    /*
     tools.GET(<#T##URLString: String##String#>, parameters: <#T##AnyObject?#>, progress: <#T##((NSProgress) -> Void)?##((NSProgress) -> Void)?##(NSProgress) -> Void#>, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: <#T##((NSURLSessionDataTask?, NSError) -> Void)?##((NSURLSessionDataTask?, NSError) -> Void)?##(NSURLSessionDataTask?, NSError) -> Void#>)
     */
    
    func request(method: HTTPMethods,urlString: String, parameters: AnyObject?,finished: (responseObject: AnyObject?, error: NSError?) -> () ) {
        
        //定义闭包参数 将相同的闭包当做参数传递
        let success = { (task: NSURLSessionDataTask, result: AnyObject?) -> () in
            //执行请求成功的回调
            finished(responseObject: result, error: nil)
        }
        
        let failure = { (task: NSURLSessionDataTask?, error: NSError) -> () in
            //执行失败的回调
            finished(responseObject: nil, error: error)
        }
        
        if method == .GET {
            
            GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
//            // "_" 表示忽略
//            GET(urlString, parameters: parameters, progress: nil, success: { (_, result) in
//                //执行请求成功的回调
//                finished(responseObject: result, error: nil)
//                }, failure: { (_, error) in
//                    //执行失败的回调
//                    finished(responseObject: nil, error: error)
//            })
        } else {
            
            POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
//            POST(urlString, parameters: parameters, progress: nil, success: { (_, result) in
//                //执行请求成功的回调
//                finished(responseObject: result, error: nil)
//                }, failure: { (_, error) in
//                    //执行失败的回调
//                    finished(responseObject: nil, error: error)
//            })
        }
    }
    
}

extension HMNetworkTools {
    func loadAccessToken(code: String, finished: (responseObject: AnyObject?, error: NSError?) -> () ) {
        let parameter = ["client_id" : client_id, "client_secret":client_secret, "grant_type": "authorization_code","code":code,"redirect_uri":redirect_uri]
        
        request(.POST, urlString: API.loadAccessToken, parameters: parameter, finished: finished)
    }
    
    
    func loadUserInfo(response: [String : AnyObject], finished: (responseObject: AnyObject?, error: NSError?) -> () ) {
        let token = response["access_token"]!
        
        let uid = response["uid"]!
        
        let parameters = ["access_token" : token,"uid" : uid]
        request(.GET, urlString: API.loadUserInfo, parameters: parameters, finished: finished)
    }
}

//首页相关
extension HMNetworkTools {
    
    func loadHomePageData(finished: (responseObject: AnyObject?, error: NSError?) -> () ) {
        guard let token = XNUserAccountViewModel.sharedAccountViewModel.userAccount?.access_token else {
            return
        }
        
        let parameters = ["access_token" : token]
        
        request(.GET, urlString: API.loadHomedata, parameters: parameters, finished: finished)
    }
}













